package cn.edu.ujn.bmis.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.SysPara;
import cn.edu.ujn.bmis.model.UserInfo;
import cn.edu.ujn.bmis.service.SysParaManager;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("SysPara")
@Scope("prototype")
public class SysParaAction extends ActionSupport {

	private static final long serialVersionUID = -5160029115400614981L;
	private SysPara sysPara;
	private SysParaManager sysParaManager;
	private List<SysPara> sysParaList = new ArrayList<SysPara>();
	private Helper helper;
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private String ids;
	private int id;
	private String value;
	private String name;
	private String mohu;
	private String operatorId;
	private String operatorName;
	private String functionNum;

	
	
	/**
	 * 进入添加系统参数的界面
	 */
	public String beforeAddSysPara(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		return SUCCESS;
	}
	/**
	 * 添加系统参数
	 */
	public void addSysPara() {
		if (sysParaManager.saveSysPara(sysPara)) {
			Helper.out("200", "系统参数添加成功", "closeCurrent", "", "","manageSysPara");
		} else {
			Helper.out("300", "系统参数添加失败", "", "", "", "");
		}
	}

	/**
	 * 删除系统参数
	 */
	public void deleteSysPara() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
		String hql = "";
		hql = "delete from SysPara s where id in (" + ids + ")";
		if (sysParaManager.update(hql)) {
			Helper.out("200", "系统参数删除成功", "", "", "", "manageSysPara");
		} else {
			Helper.out("300", "系统参数删除失败", "", "", "", "");
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findBookType(){
		String hql="";
		hql="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql);
		//操作员信息
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		operatorId = users.getUserId();
		operatorName = users.getUserName();
		return SUCCESS;
	}

	/**
	 * 查看系统参数
	 */
	@SuppressWarnings("unchecked")
	public String findSysPara() {
		String hql = "";
		String hql_size = "";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		if (name == null) {
			name = "";
		}
		if (value == null) {
			value = "";
		}
		if(mohu==null){
			mohu="";
		}
		this.currentPage = this.pageNum;
		if ("".equals(mohu)) {
			//精确查询
			if(!"".equals(name.trim())&&!"".equals(value.trim())){
				hql="from SysPara s where s.name='"+name.trim()+"' and s.value='"+value.trim()+"'";
				hql_size = "select count(*) from SysPara s where s.name='"+name.trim()+"' and s.value='"+value.trim()+"'";
			}else if(!"".equals(name.trim())&&"".equals(value.trim())){
				hql="from SysPara s where s.name='"+name.trim()+"'";
				hql_size = "select count(*) from SysPara s where s.name='"+name.trim()+"'";
			}else if("".equals(name.trim())&&!"".equals(value.trim())){
				hql="from SysPara s where s.value='"+value.trim()+"'";
				hql_size = "select count(*) from SysPara s where s.value='"+value.trim()+"'";
			}else if("".equals(name.trim())&&"".equals(value.trim())){
				hql="from SysPara";
				hql_size = "select count(*) from SysPara";
			}
		} else {
			// 模糊查询
			hql = "from SysPara s where s.name like '%" + name.trim()
					+ "%' and s.value like '%" + value.trim() + "%'";
			hql_size = " select count(*) from SysPara s where s.name like '%"
					+ name.trim() + "%' and s.value like '%" + value.trim()
					+ "%'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		sysParaList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	/**
	 * 修改前的查找
	 */
	public String beforeupdateSysPara() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql = "from SysPara s where s.id='" + id + "'";
		sysParaList = sysParaManager.findSysPara(hql);
		if (sysParaList.size() > 0) {
			sysPara = sysParaList.get(0);
		}
		return SUCCESS;
	}

	/**
	 * 修改系统参数
	 */
	public void updateSysPara() {
		if (sysParaManager.update(sysPara)) {
			Helper.out("200", "系统参数修改成功", "closeCurrent", "", "",
					"manageSysPara");
		} else {
			Helper.out("300", "系统参数修改失败", "", "", "", "");
		}
	}

	public SysPara getSysPara() {
		return sysPara;
	}

	public void setSysPara(SysPara sysPara) {
		this.sysPara = sysPara;
	}

	public SysParaManager getSysParaManager() {
		return sysParaManager;
	}

	@Resource(name = "sysParaManager")
	public void setSysParaManager(SysParaManager sysParaManager) {
		this.sysParaManager = sysParaManager;
	}

	public List<SysPara> getSysParaList() {
		return sysParaList;
	}

	public void setSysParaList(List<SysPara> sysParaList) {
		this.sysParaList = sysParaList;
	}

	public Helper getHelper() {
		return helper;
	}

	@Resource(name = "helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMohu() {
		return mohu;
	}

	public void setMohu(String mohu) {
		this.mohu = mohu;
	}

	public String getOperatorId() {
		return operatorId;
	}

	public void setOperatorId(String operatorId) {
		this.operatorId = operatorId;
	}

	public String getOperatorName() {
		return operatorName;
	}

	public void setOperatorName(String operatorName) {
		this.operatorName = operatorName;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}
	
}
